import store from './store'
import router from './router'
import {getToken} from "./utils/auth.js";
import { Notify } from 'quasar';

const whiteList=['/login','/403','404'];//白名单网页

router.beforeEach(async (to,from,next)=>{
    const hasToken=getToken();

    if (hasToken){
        if (to.path ==='/login'){  //在有token的情况下访问login页面直接跳转到根路径
            next({path:'/'});
        }else{
            const currentUser=store.state.user.currentUser;
            const adminRole=currentUser.roles.find(item=>{
                return item.name==='ROLE_ADMIN'
            })
            if (adminRole){
                next();
            }else{
                await store.dispatch('user/logout');
                Notify.create({
                    type:'negative',
                    message:'你无权登录',
                    position:'top'
                });
                next('/login');
            }

        }
    }else{
        if (whiteList.indexOf(to.path)!==-1){  //如果是白名单里面的网页就放行
            next();
        }else{
            next(`/login?redirect=${to.path}`);
        }
    }
})
